home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Mac2Win / Sample / MiniEdit(original) / mini.print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-26  |  2.9 KB  |  144 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     mini.print.c
  4.     
  5.     printing functions for Miniedit
  6.     
  7. *********************************************************************/
  8.  
  9. #include <Printing.h> 
  10. #include "mini.print.h"
  11. #include "pleasewait.h"
  12.  
  13. #define topMargin 20
  14. #define leftMargin 20
  15. #define bottomMargin 20
  16. #define tabChar    ((char)'\t')
  17.  
  18. static    THPrint    hPrint = NULL;
  19. static    int        tabWidth;
  20.  
  21.  
  22.  
  23.     /**
  24.      **        Prototypes for private functions.
  25.      **        (They really should be static.)
  26.      **
  27.      **/
  28.  
  29. void CheckPrintHandle(void);
  30. void MyDrawText(char *p, int count);
  31. void PrDoc(char **hText, long count, THPrint hPrint, int font, int size);
  32. int HowMany(void);
  33.  
  34.  
  35. void CheckPrintHandle()
  36. {
  37.     if (hPrint==NULL) 
  38.         PrintDefault(hPrint = (TPrint **) NewHandle(sizeof( TPrint)));
  39. }
  40.  
  41. void DoPageSetUp()
  42. {
  43.     PrOpen();
  44.     CheckPrintHandle();
  45.     if (PrStlDialog(hPrint)) ;
  46.     PrClose();
  47. }
  48.  
  49.  
  50. void MyDrawText(char    *p, int count)
  51. {
  52.     register char    *p1, *p2;
  53.     int                len;
  54.     Point            pt;
  55.  
  56.     p1 = p;
  57.     p2 = p+count;
  58.     while (p<p2) {
  59.         while ((p1<p2) && (*p1 !=tabChar)) *p1++;
  60.         if ((len=p1-p)>0) DrawText(p, 0, p1-p);
  61.         if (*p1==tabChar) {
  62.             GetPen(&pt);
  63.             Move((tabWidth-(pt.h-leftMargin)%tabWidth), 0);
  64.             *p1++ ;
  65.         }
  66.         p = p1;
  67.     }
  68. }
  69.  
  70. void PrDoc (char **hText, long count, THPrint hPrint, int font, int size)
  71. {
  72.     register int     line = 0;
  73.     register int     lastLineOnPage = 0;
  74.     int                length;
  75.     Rect             printRect;
  76.     int             linesPerPage;
  77.     int             lineBase;
  78.     int             lineHeight;
  79.     register char     *ptr, *p1;
  80.     FontInfo        info;
  81.     TPPrPort        printPort;
  82.  
  83.     printPort = PrOpenDoc(hPrint, 0L, 0L);
  84.     SetPort((GrafPtr)printPort);
  85.     TextFont(font);
  86.     TextSize(size);
  87.     printRect = (**hPrint).prInfo.rPage;
  88.     GetFontInfo(&info);
  89.     lineHeight = info.leading+info.ascent+info.descent;
  90.     linesPerPage = 
  91.         (printRect.bottom-printRect.top-topMargin-bottomMargin)/lineHeight;
  92.     HLock(hText);
  93.     ptr = p1 = (*hText);
  94.     do {
  95.         PrOpenPage(printPort, 0L);
  96.         lastLineOnPage += linesPerPage;
  97.         MoveTo( printRect.left+leftMargin, 
  98.             (lineBase = printRect.top+lineHeight) );
  99.         do {
  100.             /* PrintLine: */
  101.             while ((ptr<=(*hText)+count) && (*ptr++ != (char)'\r')) ;
  102.             if ((length=(int)(ptr-p1)-1)>0)
  103.                 MyDrawText(p1, length);
  104.             MoveTo( printRect.left+leftMargin, (lineBase += lineHeight));
  105.             p1 = ptr;
  106.         } while ((++line != lastLineOnPage) && (ptr<(*hText)+count));
  107.         PrClosePage(printPort);
  108.     } while (ptr<(*hText)+count);
  109.     HUnlock(hText);
  110.     PrCloseDoc(printPort);
  111. }
  112.  
  113. void PrintText(char    **hText, long length, GrafPtr gp, int tabPixels)
  114.  
  115. {
  116.     TPPrPort    printPort;
  117.     GrafPtr        savePort;
  118.     TPrStatus    prStatus;
  119.     int            copies;
  120.     
  121.     PrOpen();
  122.     CheckPrintHandle();
  123.     tabWidth = tabPixels;
  124.     SetCursor(&qd.arrow);
  125.     if (PrJobDialog(hPrint) != 0) {
  126.         PleaseWait();
  127.         GetPort(&savePort);
  128.         for (copies=HowMany(); copies>0; copies--) {
  129.             PrDoc (hText, length, hPrint, (*gp).txFont, (*gp).txSize);
  130.             PrPicFile(hPrint, 0L, 0L, 0L, &prStatus);
  131.         }
  132.         SetPort(savePort);
  133.     }
  134.     PrClose();
  135. }
  136.  
  137. int HowMany(void)
  138. {
  139.     return( ((**hPrint).prJob.bJDocLoop==bDraftLoop) ? 
  140.                 (**hPrint).prJob.iCopies : 1 );
  141. }
  142.  
  143.  
  144.